import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userToken: window.localStorage.getItem('userToken') || null,
    user: JSON.parse(window.localStorage.getItem('userInfo') || null),
    userRole: window.localStorage.getItem('userRole') || null,
  },
  mutations: {
    // 存储用户数据
    setUserToken(state, payload) {
      state.userToken = payload
      window.localStorage.setItem('userToken', payload)
    },
    setUserRole(state, payload) {
      state.userRole = payload
      window.localStorage.setItem('userRole', payload)
    },
    // 存储用户数据
    setUser(state, payload) {
      // 将 payload 转换为对象后再进行存储
      state.user = payload
      // 将 payload 数据添加到本地存储中
      window.localStorage.setItem('userInfo', JSON.stringify(payload))
    }
  },
  actions: {
  },
  modules: {
  }
})
